﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using IBM.Data.Informix;

namespace VPBS.Common
{
    public class InformixHelper
    {
        public InformixHelper()
        {
        }

        public static DataSet ExecuteDataSet(string connectionString, CommandType commandType, string commandText)
        {
            DataSet dataSet = new DataSet("ResultDataSet");
            using (IfxConnection connection = new IfxConnection(connectionString))
            {
                IfxException exception;
                try
                {
                    connection.Open();
                }
                catch (IfxException exception2)
                {
                    exception = exception2;
                    throw new Exception("Open Informix Connection", exception);
                }
                using (IfxCommand command = new IfxCommand(commandText, connection))
                {
                    command.CommandType = commandType;
                    command.CommandTimeout = 0;
                    using (IfxDataAdapter adapter = new IfxDataAdapter(command))
                    {
                        try
                        {
                            adapter.Fill(dataSet);
                        }
                        catch (Exception exception3)
                        {
                            throw new Exception("Fill From Informix", exception3);
                        }
                    }
                }
                try
                {
                    connection.Close();
                }
                catch (IfxException exception4)
                {
                    exception = exception4;
                    throw new Exception("Close Informix Connection", exception);
                }
            }
            return dataSet;
        }

        public static DataSet ExecuteDataSet(string connectionString, CommandType commandType, string commandText, params IfxParameter[] commandParameters)
        {
            DataSet dataSet = new DataSet("ResultDataSet");
            using (IfxConnection connection = new IfxConnection(connectionString))
            {
                IfxException exception;
                try
                {
                    connection.Open();
                }
                catch (IfxException exception2)
                {
                    exception = exception2;
                    throw new Exception("Open Informix Connection", exception);
                }
                using (IfxCommand command = new IfxCommand(commandText, connection))
                {
                    command.CommandType = commandType;
                    foreach (IfxParameter parameter in commandParameters)
                    {
                        command.Parameters.Add(parameter);
                    }
                    using (IfxDataAdapter adapter = new IfxDataAdapter(command))
                    {
                        try
                        {
                            adapter.Fill(dataSet);
                        }
                        catch (Exception exception3)
                        {
                            throw new Exception("Fill From Informix", exception3);
                        }
                    }
                }
                try
                {
                    connection.Close();
                }
                catch (IfxException exception4)
                {
                    exception = exception4;
                    throw new Exception("Close Informix Connection", exception);
                }
            }
            return dataSet;
        }

        public static DataSet ExecuteDataSet(string connectionString, CommandType commandType, string commandText, params object[] commandParameters)
        {
            DataSet dataSet = new DataSet("ResultDataSet");
            using (IfxConnection connection = new IfxConnection(connectionString))
            {
                IfxException exception;
                try
                {
                    connection.Open();
                }
                catch (IfxException exception2)
                {
                    exception = exception2;
                    throw new Exception("Open Informix Connection", exception);
                }
                using (IfxCommand command = new IfxCommand(commandText, connection))
                {
                    command.CommandType = commandType;
                    foreach (object obj2 in commandParameters)
                    {
                        command.Parameters.Add(obj2);
                    }
                    using (IfxDataAdapter adapter = new IfxDataAdapter(command))
                    {
                        try
                        {
                            adapter.Fill(dataSet);
                        }
                        catch (Exception exception3)
                        {
                            throw new Exception("Fill From Informix", exception3);
                        }
                    }
                }
                try
                {
                    connection.Close();
                }
                catch (IfxException exception4)
                {
                    exception = exception4;
                    throw new Exception("Close Informix Connection", exception);
                }
            }
            return dataSet;
        }

        public static int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText)
        {
            int num = 0;
            using (IfxConnection connection = new IfxConnection(connectionString))
            {
                IfxException exception;
                try
                {
                    connection.Open();
                }
                catch (IfxException exception2)
                {
                    exception = exception2;
                    throw new Exception("Open Informix Connection", exception);
                }
                using (IfxCommand command = new IfxCommand(commandText, connection))
                {
                    command.CommandType = commandType;
                    try
                    {
                        num = command.ExecuteNonQuery();
                    }
                    catch (Exception exception3)
                    {
                        throw new Exception("Fill From Informix", exception3);
                    }
                }
                try
                {
                    connection.Close();
                }
                catch (IfxException exception4)
                {
                    exception = exception4;
                    throw new Exception("Close Informix Connection", exception);
                }
            }
            return num;
        }

        public static int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText, params IfxParameter[] commandParameters)
        {
            int num = 0;
            using (IfxConnection connection = new IfxConnection(connectionString))
            {
                IfxException exception;
                try
                {
                    connection.Open();
                }
                catch (IfxException exception2)
                {
                    exception = exception2;
                    throw new Exception("Open Informix Connection", exception);
                }
                using (IfxCommand command = new IfxCommand(commandText, connection))
                {
                    command.CommandType = commandType;
                    foreach (IfxParameter parameter in commandParameters)
                    {
                        command.Parameters.Add(parameter);
                    }
                    try
                    {
                        num = command.ExecuteNonQuery();
                    }
                    catch (Exception exception3)
                    {
                        throw new Exception("Fill From Informix", exception3);
                    }
                }
                try
                {
                    connection.Close();
                }
                catch (IfxException exception4)
                {
                    exception = exception4;
                    throw new Exception("Close Informix Connection", exception);
                }
            }
            return num;
        }

        public static int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText, params object[] commandParameters)
        {
            int num = 0;
            using (IfxConnection connection = new IfxConnection(connectionString))
            {
                IfxException exception;
                try
                {
                    connection.Open();
                }
                catch (IfxException exception2)
                {
                    exception = exception2;
                    throw new Exception("Open Informix Connection", exception);
                }
                using (IfxCommand command = new IfxCommand(commandText, connection))
                {
                    command.CommandType = commandType;
                    foreach (object obj2 in commandParameters)
                    {
                        command.Parameters.Add(obj2);
                    }
                    try
                    {
                        num = command.ExecuteNonQuery();
                    }
                    catch (Exception exception3)
                    {
                        throw new Exception("Fill From Informix", exception3);
                    }
                }
                try
                {
                    connection.Close();
                }
                catch (IfxException exception4)
                {
                    exception = exception4;
                    throw new Exception("Close Informix Connection", exception);
                }
            }
            return num;
        }

        public static IfxDataReader ExecuteReader(IfxConnection connection, CommandType commandType, string commandText)
        {
            IfxDataReader reader;
            try
            {
                if (connection.State != ConnectionState.Open)
                {
                    connection.Open();
                }
            }
            catch (IfxException exception)
            {
                throw new Exception("Open Ifx Connection", exception);
            }
            IfxCommand command = new IfxCommand(commandText, connection);
            command.CommandType = commandType;
            try
            {
                reader = command.ExecuteReader();
            }
            catch (Exception exception2)
            {
                throw new Exception("Execute reader From Informix", exception2);
            }
            return reader;
        }
    }
}